package com.skyinno.mrms.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class SerializableUtil {

	/**
	 * 序列化
	 * @param o
	 * @return
	 */
	public static String serialize(Object o) {
		ObjectOutputStream oos = null;
		// 序列化后数据流给ByteArrayOutputStream 来保存。
		// ByteArrayOutputStream 可转成字符串或字节数组
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			// byte[] myb= "s ";
			oos = new ObjectOutputStream(baos);
			oos.writeObject(o);
			byte[] byteValue = baos.toByteArray();
			oos.close();
			return new String(byteValue,"ISO-8859-1");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			System.out.println("序列化时产生错误 ");
		}
		return null;
	}

	/**
	 * 反序列化
	 * @param string
	 * @return
	 */
	public static Object deSerialize(String string) {
		// ByteArrayInputStream 可接收一个字节数组 "byte[] "。供反序列化做参数
		ByteArrayInputStream bais = null;
		// 反序列化使用的输入流
		ObjectInputStream ois = null;
		try {
			byte[] byteValue = string.getBytes("ISO-8859-1");
			bais = new ByteArrayInputStream(byteValue);
			ois = new ObjectInputStream(bais);
			Object object = ois.readObject();
			ois.close();
			return object;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			System.out.println("反序列化时产生错误 ");
		}
		return null;
		
		
		
	}

}
